home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / archiver / unix / zip19p1.zoo / fileio.c < prev    next >
C/C++ Source or Header  |  1992-08-26  |  57KB  |  2,108 lines

  1. /*
  2.  
  3.  Copyright (C) 1990-1992 Mark Adler, Richard B. Wales, Jean-loup Gailly,
  4.  Kai Uwe Rommel and Igor Mandrichenko.
  5.  Permission is granted to any individual or institution to use, copy, or
  6.  redistribute this software so long as all of the original files are included
  7.  unmodified, that it is not sold for profit, and that this copyright notice
  8.  is retained.
  9.  
  10. */
  11.  
  12. /*
  13.  *  fileio.c by Mark Adler.
  14.  */
  15.  
  16. #include "zip.h"
  17.  
  18. #include <time.h>
  19.  
  20. #ifdef WIN32
  21. #  include <sys/utime.h>
  22. #  include <windows.h> /* for findfirst/findnext */
  23. #endif
  24.  
  25. #ifdef MACOS
  26. #  define EXDEV 1
  27. #endif
  28.  
  29. #ifdef OSF
  30. #  define EXDEV 18    /* avoid a bug in the DEC OSF/1 header files. */
  31. #else
  32. #  include <errno.h>
  33. #endif
  34.  
  35. #ifdef MINIX
  36. #  ifdef S_IWRITE
  37. #    undef S_IWRITE
  38. #  endif /* S_IWRITE */
  39. #  define S_IWRITE S_IWUSR
  40. #endif /* S_IWUSR */
  41.  
  42. #ifdef ATARI_ST
  43. #  undef MSDOS
  44. #endif
  45.  
  46. #ifdef MSDOS
  47. #  include <io.h>
  48. #  if (defined(__TURBOC__) || defined(__GO32__))
  49. #    include <dir.h>
  50. #  else /* !__TURBOC__ */
  51. #    if !defined(__EMX__) && !defined(__WATCOMC__)
  52. #      include <direct.h>
  53. #    endif
  54. #  endif /* ?__TURBOC__ */
  55. #  define link rename
  56. #  ifdef OS2
  57. #    define MATCH shmatch
  58. #  else /* !OS2 */
  59. #    define MATCH dosmatch
  60. #  endif /* ?OS2 */
  61. #else /* !MSDOS */
  62.    extern int errno;    /* error number from system functions */
  63. #  ifdef VMS
  64. #    define RMDIR
  65. #    define link rename
  66. #    include "VMSmunch.h"
  67. #  endif /* VMS */
  68. #  ifdef MACOS
  69. #    define link rename
  70. #    define mktemp tmpnam
  71. #  endif
  72. #  define MATCH shmatch
  73. #endif /* ?MSDOS */
  74.  
  75. #ifdef ATARI_ST
  76. #  define MSDOS 1
  77. #endif
  78.  
  79. #ifdef UTS
  80. #  define RMDIR
  81. #endif /* UTS */
  82.  
  83.  
  84. /* Extra malloc() space in names for cutpath() */
  85. #ifdef VMS
  86. #  define PAD 3         /* may have to change .FOO] to ]FOO.DIR */
  87. #else /* !VMS */
  88. #  define PAD 0
  89. #endif /* ?VMS */
  90.  
  91.  
  92. /* For now, assume DIRENT implies System V implies TERMIO */
  93. #if defined(DIRENT) && !defined(MINIX) && !defined(TERMIO)
  94. #  define TERMIO
  95. #endif
  96.  
  97.  
  98. #ifdef CRYPT
  99. #  ifdef MSVMS
  100. #    ifdef MSDOS
  101. #      ifdef __EMX__
  102. #        define getch() _read_kbd(0, 1, 0)
  103. #      else
  104. #        ifdef __GO32__
  105. #          include <pc.h>
  106. #          define getch() getkey()
  107. #        else
  108. #          include <conio.h>
  109. #        endif
  110. #      endif
  111. #    else /* !MSDOS */
  112. #      define getch() getc(stderr)
  113. #      define echoff(f) echo(0)   /* for echo control */
  114. #      define echon()   echo(1)
  115. #      include <iodef.h>
  116. #      include <ttdef.h>
  117. #      if !defined(SS$_NORMAL)
  118. #        define SS$_NORMAL 1   /* only thing we need from <ssdef.h> */
  119. #      endif
  120. #    endif /* ?MSDOS */
  121. #  else /* !MSVMS */
  122. #    ifdef TERMIO       /* Amdahl, Cray, all SysV? */
  123. #      ifdef CONVEX
  124. #        include <sys/termios.h>
  125. #        include <sgtty.h>
  126. #        define O_BINARY 0
  127. #      else /* !CONVEX */
  128. #        ifdef LINUX 
  129. #          include <termios.h>
  130. #        else /* !LINUX */
  131. #          include <sys/termio.h>
  132. #        endif /* ?LINUX */
  133. #        define sgttyb termio
  134. #        define sg_flags c_lflag
  135.          int ioctl OF((int, int, voidp *));
  136. #      endif /* ?CONVEX */
  137. #      define GTTY(f,s) ioctl(f,TCGETA,s)
  138. #      define STTY(f,s) ioctl(f,TCSETAW,s)
  139. #    else /* !TERMIO */
  140. #      ifndef MINIX
  141. #        include <sys/ioctl.h>
  142. #      endif /* !MINIX */
  143. #      include <sgtty.h>
  144.        int gtty OF((int, struct sgttyb *));
  145.        int stty OF((int, struct sgttyb *));
  146. #      define GTTY gtty
  147. #      define STTY stty
  148. #    endif /* ?TERMIO */
  149.      int isatty OF((int));
  150.      char *ttyname OF((int));
  151.      int open OF((char *, int, ...));
  152.      int close OF((int));
  153.      int read OF((int, voidp *, int));
  154. #  endif /* ?MSVMS */
  155. #endif /* ?CRYPT */
  156.  
  157. #ifdef VMS
  158. #  include <descrip.h>
  159. #  include <rms.h>
  160. #endif
  161.  
  162. /* For directory access. (This is getting rather messy. Cleanup
  163.  * scheduled for version 17.9.)
  164.  */
  165. #ifndef UTIL
  166.  
  167. #ifdef SYSV                     /* use readdir()  */
  168. #  include <dirent.h>
  169. #  define dstrm DIR
  170. #  define direct dirent
  171. #else
  172.  
  173. #ifdef DIRENT                   /* use getdents() */
  174. #  if defined(MINIX) || defined(OSF)
  175. #    include <dirent.h>
  176. #  else /* !MINIX */
  177. #    include <sys/dirent.h>
  178. #  endif /* ?MINIX */
  179. #  define direct dirent
  180. #  ifdef MINIX
  181.      int getdents OF((int, char *, unsigned));
  182. #  else /* !MINIX */
  183.      int getdents OF((int, char *, int));
  184. #  endif /* ?MINIX */
  185. #  define DBSZ 4096     /* This has to be bigger than a directory block */
  186.    typedef struct {     /* directory stream buffer */
  187.      int f;             /* file descriptor for the directory "file" */
  188.      char *p;           /* pointer to next entry in buffer */
  189.      char *q;           /* pointer after end of buffer contents */
  190.      char b[DBSZ];              /* buffer */
  191.    } dstrm;
  192.  
  193. #else /* !DIRENT */             /* use opendir(), etc. */
  194. #  if defined(CONVEX) || defined(ultrix)
  195. #    include <dirent.h>
  196. #    ifdef direct
  197. #      undef direct /* ultrix 4.2, at least if !__POSIX */
  198. #    endif
  199. #    define direct dirent
  200. #  endif /* CONVEX || ultrix */
  201. #  ifdef NDIR
  202. #    include "ndir.h"           /* for HPUX */
  203. #  else /* !NDIR */
  204. #    ifdef MSDOS
  205. #     ifdef OS2
  206. #      include "os2zip.h"
  207. #     else /* !OS2 */
  208. #      ifndef ATARI_ST
  209. #        include <dos.h>
  210. #      endif
  211. #      if (defined(__TURBOC__) || defined(__GO32__))
  212. #        define FATTR           FA_HIDDEN+FA_SYSTEM+FA_DIREC
  213. #        define FFIRST(n,d)     findfirst(n,(struct ffblk *)d,FATTR)
  214. #        define FNEXT(d)        findnext((struct ffblk *)d)
  215. #      else /* !__TURBOC__ */
  216. #        define FATTR           _A_HIDDEN+_A_SYSTEM+_A_SUBDIR
  217. #        define FFIRST(n,d)     _dos_findfirst(n,FATTR,(struct find_t *)d)
  218. #        define FNEXT(d)        _dos_findnext((struct find_t *)d)
  219. #      endif /* ?__TURBOC__ */
  220.        typedef struct direct {
  221.          char d_reserved[30];
  222.          char d_name[13];
  223.      int d_first;
  224. #ifdef WIN32
  225.      HANDLE d_hFindFile;
  226. #endif
  227.        } DIR;
  228. #     endif /* ?OS2 */
  229. #    else /* !MSDOS */
  230. #      ifdef VMS
  231. #        include <ssdef.h>
  232.          typedef struct direct {
  233.              int d_wild;                /* flag for wildcard vs. non-wild */
  234.              struct FAB fab;
  235.              struct NAM nam;
  236.              char d_qualwildname[NAM$C_MAXRSS + 1];
  237.              char d_name[NAM$C_MAXRSS + 1];
  238.          } DIR;
  239. #      else /* !VMS */
  240. #        ifdef MACOS
  241.            typedef struct direct {
  242.              int d_wild;                /* flag for wildcard vs. non-wild */
  243.              char *d_name;
  244.           } DIR;
  245. #        endif
  246. #        ifdef M_XENIX
  247. #          include <sys/ndir.h>
  248. #        else /* !M_XENIX */
  249. #          include <sys/dir.h>
  250. #        endif /* ?M_XENIX */
  251. #        ifdef NODIR                    /* for AT&T 3B1 */
  252. #          define dirent direct
  253.            typedef FILE DIR;
  254. #          define dstrm DIR
  255. #        endif /* NODIR */
  256. #      endif /* ?VMS */
  257. #    endif /* ?MSDOS */
  258. #  endif /* ?NDIR */
  259. #  define dstrm DIR
  260. #  ifndef NODIR
  261.      DIR *opendir OF((char *));
  262. #  endif /* !NODIR */
  263. #  ifndef CONVEX
  264.      struct direct *readdir OF((DIR *));
  265. #  endif /* !CONVEX */
  266. #endif /* ?DIRENT */
  267. #endif /* ?SYSV */
  268. #endif /* !UTIL */
  269.  
  270.  
  271. /* Library functions not in (most) header files */
  272.  
  273. #if defined(__IBMC__) || defined(__WATCOMC__)
  274. #  define NO_MKTEMP
  275. #endif
  276. char *mktemp OF((char *));
  277.  
  278. #ifdef __GO32__
  279.   char *strlwr OF((char *));
  280. #else
  281.   int link OF((char *, char *));
  282.   int unlink OF((char *));
  283. # if defined(MSDOS)
  284.    int chmod OF((char *, int));
  285.    /* For many targets, chmod is already defined by sys/stat.h, and second
  286.     * parameter is an unsigned long.
  287.     */
  288. # endif
  289. #endif
  290.  
  291.  
  292. #ifndef UTIL    /* the companion #endif is a bit of ways down ... */
  293.  
  294. #ifndef __TURBOC__
  295.    int utime OF((char *, time_t *));
  296. #endif /* !__TURBOC__ */
  297. #ifndef MSDOS
  298.    int open OF((char *, int, ...));
  299.    int close OF((int));
  300. #  ifndef RMDIR
  301.      int rmdir OF((char *));
  302. #  endif /* !RMDIR */
  303. #endif /* !MSDOS */
  304.  
  305.  
  306. /* Local globals (kinda like "military intelligence" or "broadcast quality") */
  307. local int exflag = 0;           /* Exclude flag */
  308.  
  309. #ifdef VMS
  310.   typedef int statime;
  311. #else /* !VMS */
  312.   typedef time_t statime;
  313. #endif /* ?VMS */
  314.  
  315. /* Local functions */
  316. #ifdef PROTO
  317. #  ifdef VMS
  318.      local void vms_wild(char *, dstrm *);
  319. #  endif /* VMS */
  320. #  ifdef DIRENT
  321.      local dstrm *opend(char *);
  322.      local void close